package co.th.bjc.md.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import co.th.bjc.md.dao.ImpRating;
import co.th.bjc.md.dao.Person;
import co.th.bjc.md.dao.Product;
import co.th.bjc.md.dao.RpRating;
import co.th.bjc.md.process.service.CallService;
import co.th.bjc.md.service.interfaces.ProductManageService;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class MemberManageServlet
 */
public class MemberManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=UTF-8");
		JSONArray jsonObject = new JSONArray();
		boolean status = false;
		String action = request.getParameter("action")==null?"":request.getParameter("action");
		
		if(action.equals("SEARCH")){
			PrintWriter out = response.getWriter();
			jsonObject = this.getSearchResult(request, response);
			out.print(jsonObject);
		}else if(action.equals("DELETE")){
			// Delete process
			if(this.delete(request, response)){
				PrintWriter out = response.getWriter();
				status = true;
				jsonObject = this.getSearchResult(request, response);
				jsonObject.add(status);
				out.print(jsonObject);
			}
		}else if(action.equals("GETEDIT")){
			PrintWriter out = response.getWriter();
			jsonObject = getEditItem(request, response);
			status = true;
			jsonObject.add(status);
			out.print(jsonObject);
		}else{
			getServletContext().getRequestDispatcher("/jsp/datamanage/manage_member_main.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=UTF-8");
		Hashtable multipartParams;
		try {
			multipartParams = HospitalManageServlet.getParamsFromMultipartForm(request);
			Person person = new Person();
			person.setPersonCode(BigDecimal.valueOf(Long.parseLong((String)multipartParams.get("person_code"))));
			person.setPersonFirstName((String)multipartParams.get("ins_person_firstname"));
			person.setPersonLastName((String)multipartParams.get("ins_person_lastname"));
			person.setPersonTitle((String)multipartParams.get("ins_person_title"));
			person.setPersonType(Integer.parseInt((String)multipartParams.get("ins_type")));
			person.setEmail((String)multipartParams.get("ins_email"));
			person.setTelephone((String)multipartParams.get("ins_telephone"));
			person.setRemark((String)multipartParams.get("ins_imp_rate_person"));
			ImpRating imp = new ImpRating();
			imp.setImpCode(Integer.parseInt((String)multipartParams.get("ins_imp_rate_person")));
			person.setImp(imp);
			
			RpRating rp = new RpRating();
			rp.setRpCode(Integer.parseInt((String)multipartParams.get("ins_rp_rate_person")));
			person.setRp(rp);
			
			System.out.println("Success");
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static Hashtable getParamsFromMultipartForm(HttpServletRequest req) throws FileUploadException {
        Hashtable ret = new Hashtable();
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
        for (FileItem item : items) {
            if (item.isFormField()) {
                ret.put(item.getFieldName(), item.getString());
            }
        }
        return ret;
    }
	
	private JSONArray getSearchResult(HttpServletRequest request, HttpServletResponse response) {
		String srhName = request.getParameter("srh_first_last_name") == null?"":request.getParameter("srh_first_last_name");
		String srhType = request.getParameter("srh_type") == null?"":request.getParameter("srh_type");
		List<Person> list = new ArrayList<Person>();
		Person obj = new Person();
		obj.setPersonCode(BigDecimal.valueOf(Long.parseLong("1")));
		obj.setPersonTitle("นพ.");
		obj.setPersonFirstName("มานะ");
		obj.setPersonLastName("สายลมหวน");
		obj.setEmail("mana.s@gmail.com");
		obj.setTelephone("0918293847");
		obj.setPersonType(1);
		list.add(obj);
		
		Person obj1 = new Person();
		obj1.setPersonCode(BigDecimal.valueOf(Long.parseLong("1")));
		obj1.setPersonTitle("นพ.");
		obj1.setPersonFirstName("สายป่าน");
		obj1.setPersonLastName("สายลมหวน");
		obj1.setEmail("saiparn.s@gmail.com");
		obj1.setTelephone("0880827162");
		obj1.setPersonType(2);
		list.add(obj1);
		JSONArray jsonObject = JSONArray.fromObject(list);
		return jsonObject;
	}
	
	private boolean delete(HttpServletRequest request, HttpServletResponse response){
		BigDecimal id = BigDecimal.valueOf(Long.parseLong(request.getParameter("id")));
		return false;
	}
	
	private JSONArray getEditItem(HttpServletRequest request, HttpServletResponse response){
		BigDecimal id = BigDecimal.valueOf(Long.parseLong(request.getParameter("id")));
		Person obj = new Person();
		obj.setPersonCode(BigDecimal.valueOf(Long.parseLong("1")));
		obj.setPersonTitle("นพ.");
		obj.setPersonFirstName("มานะ");
		obj.setPersonLastName("สายลมหวน");
		obj.setEmail("mana.s@gmail.com");
		obj.setTelephone("0918293847");
		ImpRating imp = new ImpRating();
		imp.setImpCode(3);
		obj.setImp(imp);
		RpRating rp = new RpRating();
		rp.setRpCode(4);
		obj.setRp(rp);
		obj.setRemark("Other remark.");
		obj.setPersonType(1);
		JSONArray json = JSONArray.fromObject(obj);
		return json;
	}

}
